Shader "HL/Toon CompleteHL"
{
	Properties
	{
		_Color("Color", Color) = (1,1,1,1)
		_MainTex("Main Texture", 2D) = "white" {}

		_AmbienceColor("Ambience Color", Color) = (0.1,0.1,0.1,1)
		_SpecularColor("Specular Color", Color) = (0.9,0.9,0.9,1)

		_Glossiness("Glossiness", Float) = 32
		_RimColor("Rim Color", Color) = (1,1,1,1)
		_RimAmount("Rim Amount", Range(0, 1)) = 0.6
		//边缘光强度
		_RimPower("Rim Power", Range(0, 1)) = 0.68

		_RimThreshold("Rim Threshold", Range(0, 1)) = 0.1		
		//灯光强度
		_LightThresHold("Light Threshold", Range(0.5, 5)) = 1
		//阴影颜色
		_ShadowColor("Shadow Color", Color) = (0.6,0.4,0.3,1)
	}
	SubShader
	{
		Pass
		{
			Tags
			{
				"LightMode" = "ForwardBase"
				"PassFlags" = "OnlyDirectional"
			}

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma multi_compile_fwdbase
			
			#include "UnityCG.cginc"

			#include "Lighting.cginc"
			#include "AutoLight.cginc"

			struct appdata
			{
				float4 vertex : POSITION;				
				float4 uv : TEXCOORD0;
				float3 normal : NORMAL;
			};

			struct v2f
			{
				float4 pos : SV_POSITION;
				float3 worldNormal : NORMAL;
				float2 uv : TEXCOORD0;
				float3 viewDir : TEXCOORD1;	

				SHADOW_COORDS(2)
			};

			sampler2D _MainTex;
			float4 _MainTex_ST;
			
			v2f vert (appdata v)
			{
				v2f o;
				o.pos = UnityObjectToClipPos(v.vertex);
				o.worldNormal = UnityObjectToWorldNormal(v.normal);		
				o.viewDir = WorldSpaceViewDir(v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);

				TRANSFER_SHADOW(o)
				return o;
			}
			
			float4 _Color;

			float4 _AmbienceColor;
			float4 _SpecularColor;
			float _Glossiness;		

			float4 _RimColor;
			float4 _ShadowColor;
			float _RimAmount;
			float _RimPower;
			float _RimThreshold;	
			float _LightThresHold;

			float4 frag (v2f i) : SV_Target
			{
				float3 normal = normalize(i.worldNormal);
				float3 viewDir = normalize(i.viewDir);

				float NdotL = dot(_WorldSpaceLightPos0, normal);


				float shadow = SHADOW_ATTENUATION(i);

				float lightIntensity = smoothstep(0, 0.01, NdotL * shadow);	
				float4 light = lightIntensity * _LightColor0;
				if (lightIntensity<=0.8)
				{
					light = _ShadowColor;
				}
				
				float3 halfVector = normalize(_WorldSpaceLightPos0 + viewDir);
				float NdotH = dot(normal, halfVector);

				float specularIntensity = pow(NdotH * lightIntensity, _Glossiness * _Glossiness);
				float specularIntensitySmooth = smoothstep(0.005, 0.01, specularIntensity);
				float4 specular = specularIntensitySmooth * _SpecularColor;				

				float NdotV = abs(dot(normal, viewDir));
				float rimDot = 1 - dot(viewDir, normal);

				float rimIntensity = rimDot * pow(NdotL, _RimThreshold);
				rimIntensity = smoothstep(_RimAmount - 0.01, _RimAmount + 0.01, rimIntensity)*_RimPower;
				float4 rim = rimIntensity * _RimColor;

				float4 sample = tex2D(_MainTex, i.uv);
				float4 col = (light*_LightThresHold + _AmbienceColor + specular + rim) * _Color * sample;
				//菲涅尔效应
				half3 fCol = FresnelTerm(_RimColor, NdotV);
				float4 fresnel = float4(fCol, 1)*rimIntensity;

				return col+fresnel;
			}
			ENDCG
		}

	}
	FallBack "Standard"
	//	FallBack Off
}
